package com.lucy.viewer.vditor.controller;

import com.lucy.viewer.service.ViewerFileService;
import com.lucy.viewer.vditor.service.VditorPreHandlerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author kindear
 * vditor 相关api
 */
@Slf4j
@RestController
@RequestMapping("viewer")
@Api(tags = "vditor相关接口")
@AllArgsConstructor
public class VditorController {
    VditorPreHandlerService preHandlerService;

    ViewerFileService fileService;

    @PostMapping("vditor/upload")
    @ApiOperation(value = "vditor上传",notes = "")
    public Map<String,Object> vditorUpload(@RequestParam("file") MultipartFile file){
        Map<String,Object> resMap = new HashMap<String,Object>();
        resMap.put("success",true);
        resMap.put("code",200);
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取链接
        String url = fileService.fileUpload(file);
        // 获取预处理
        String html = preHandlerService.handle(url,fileName);
        //
        resMap.put("data",html);
        return resMap;
    }
}
